package com.llmcu.source.java8.java8inaction.chapter12;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;

/**
 * Duration常见用法
 *
 * @author liuling
 * @date 2024-12-16 17:03
 */
public class DurationDemo {
    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.of(2024, 12, 16, 17, 5, 59);
        LocalDateTime localDateTime1 = LocalDateTime.of(2024, 11, 15, 18, 0, 0);
        Instant instant = Instant.ofEpochSecond(3600 * 24);
        Instant instant1 = Instant.ofEpochSecond(3600 * 24 * 3);

        // 1. You can create a duration between two LocalTimes, two LocalDateTimes, or two Instants as follows
        Duration duration = Duration.between(localDateTime1, localDateTime);
        System.out.println(duration);

        Duration duration1 = Duration.between(instant, instant1);
        System.out.println(duration1);

        System.out.println(Duration.between(LocalTime.MIN, LocalTime.MAX));
        System.out.println(Duration.between(LocalTime.of(5, 5, 5, 100_000_000), LocalTime.of(5, 5, 6)));

        // b. 两参数不支持不同类型
        // Duration duration1 = Duration.between(localDateTime1, instant);
        // c. 不支持 LocalDate类型
        // Duration duration2 = Duration.between(localDate, LocalDate.now());

        // a. 只支持秒、纳秒两个维度展示，且只显示当前维度差距，而不是当前维度总差距
        System.out.println(duration.getSeconds());
        System.out.println(duration.get(ChronoUnit.SECONDS));
        System.out.println(duration.get(ChronoUnit.NANOS));
        System.out.println(Duration.between(LocalTime.of(5, 5, 5, 100_000_000), LocalTime.of(5, 5, 6)).getSeconds());
        System.out.println(Duration.between(LocalTime.of(5, 5, 5, 100_000_000), LocalTime.of(5, 5, 6)).get(ChronoUnit.SECONDS));
        System.out.println(Duration.between(LocalTime.of(5, 5, 5, 100_000_000), LocalTime.of(5, 5, 6)).get(ChronoUnit.NANOS));

        // 2. 通过of构建对象
        Duration duration2 = Duration.ofMillis(3_000);
        Duration duration3 = Duration.ofSeconds(3);
        System.out.println(duration2.getSeconds() == duration3.getSeconds());
        // d. 纳秒没有范围限制
        Duration duration4 = Duration.ofSeconds(1, 2_000_000_000);
        System.out.println(duration4.getSeconds());


        // 3. 通过from构建对象
        Duration duration5 = Duration.from(duration2);
        System.out.println("duration5:" + duration5);
        // e. 当前维度总差距
        System.out.println(duration5.toMillis());
        System.out.println(duration5.toNanos());
        System.out.println(duration5.toDays());
        System.out.println(duration5.toHours());

        // f. 给LocalTime、LocalDateTime、instant加上period得到新的LocalTime、LocalDateTime、instant
        Temporal temporal3 = duration.addTo(LocalDateTime.of(2024, 12, 16, 17, 5, 59));
        System.out.println(temporal3);
        System.out.println(temporal3.getClass());
        Temporal temporal4 = duration.addTo(instant);
        System.out.println(temporal4);
        System.out.println(temporal4.getClass());
        Temporal temporal5 = duration.addTo(LocalTime.of(5, 5, 6));
        System.out.println(temporal5);
        System.out.println(temporal5.getClass());

        // g. duration1-duration2=duration3
        Duration duration6 = duration.minus(Duration.ofDays(1));
        System.out.println("duration6:" + duration6);

        // h. duration*5=5duration
        Duration duration7 = duration1.multipliedBy(5);
        System.out.println("duration7:" + duration7);

        // i. 取反
        Duration duration8 = duration1.negated();
        System.out.println("duration8:" + duration8);

        // j. duration1+duration2=duration3
        Duration duration9 = duration1.plus(Duration.ofDays(3));
        System.out.println("duration9:" + duration9);

        // k. 给LocalTime、LocalDateTime、instant减去period得到新的LocalTime、LocalDateTime、instant
        Temporal temporal = duration1.subtractFrom(LocalDateTime.now());
        System.out.println("temporal:" + temporal);
        System.out.println(temporal.getClass());

    }
}
